package com.edu.exam.entity.assessment.generation;

import com.edu.exam.enums.assessment.generation.LogLevel;
import com.edu.exam.enums.assessment.generation.OperationPhase;
import com.edu.exam.enums.common.OperationType;
import com.edu.exam.enums.common.OperationResult;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 生成日志实体类
 * 所属模块：B-05 考核计划管理模块
 * 对应数据库表：generation_log
 *
 * <p>考核任务生成过程日志实体，详细记录任务生成的每个步骤、
 * 操作信息、状态变化、异常情况等。提供完整的生成过程追踪
 * 和问题诊断功能。</p>
 *
 * @author 系统生成
 * @version 2.0
 * @since 2025-11-09
 */
@Entity
@Table(name = "generation_log")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class GenerationLog {

    /**
     * 日志ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 批次ID
     */
    @Column(name = "batch_id")
    private Long batchId;

    /**
     * 批次编号
     */
    @Column(name = "batch_code", length = 50)
    private String batchCode;

    /**
     * 任务ID
     */
    @Column(name = "task_id")
    private Long taskId;

    /**
     * 任务编号
     */
    @Column(name = "task_code", length = 50)
    private String taskCode;

    /**
     * 日志级别
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "log_level", length = 10, nullable = false)
    private LogLevel logLevel;

    /**
     * 操作类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_type", length = 20, nullable = false)
    private OperationType operationType;

    /**
     * 操作阶段
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_phase", length = 20)
    private OperationPhase operationPhase;

    /**
     * 日志消息
     */
    @Column(name = "log_message", length = 2000, nullable = false)
    private String logMessage;

    /**
     * 详细信息（JSON格式）
     */
    @Column(name = "details", columnDefinition = "TEXT")
    private String details;

    /**
     * 操作结果
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "operation_result", length = 20)
    private OperationResult operationResult;

    /**
     * 执行时长（毫秒）
     */
    @Column(name = "execution_duration")
    private Long executionDuration;

    /**
     * 影响记录数
     */
    @Column(name = "affected_records")
    private Integer affectedRecords;

    /**
     * 错误代码
     */
    @Column(name = "error_code", length = 50)
    private String errorCode;

    /**
     * 错误消息
     */
    @Column(name = "error_message", length = 1000)
    private String errorMessage;

    /**
     * 异常堆栈（TEXT格式）
     */
    @Column(name = "stack_trace", columnDefinition = "TEXT")
    private String stackTrace;

    /**
     * 操作人ID
     */
    @Column(name = "operator_id")
    private Long operatorId;

    /**
     * 操作人姓名
     */
    @Column(name = "operator_name", length = 100)
    private String operatorName;

    /**
     * 操作人IP地址
     */
    @Column(name = "operator_ip", length = 50)
    private String operatorIp;

    /**
     * 操作人用户代理
     */
    @Column(name = "operator_user_agent", length = 500)
    private String operatorUserAgent;

    /**
     * 会话ID
     */
    @Column(name = "session_id", length = 100)
    private String sessionId;

    /**
     * 请求ID
     */
    @Column(name = "request_id", length = 100)
    private String requestId;

    /**
     * 线程ID
     */
    @Column(name = "thread_id", length = 50)
    private String threadId;

    /**
     * 线程名称
     */
    @Column(name = "thread_name", length = 100)
    private String threadName;

    /**
     * 上下文信息（JSON格式）
     */
    @Column(name = "context_info", columnDefinition = "TEXT")
    private String contextInfo;

    /**
     * 业务数据（JSON格式）
     */
    @Column(name = "business_data", columnDefinition = "TEXT")
    private String businessData;

    /**
     * 系统信息（JSON格式）
     */
    @Column(name = "system_info", columnDefinition = "TEXT")
    private String systemInfo;

    /**
     * 性能指标（JSON格式）
     */
    @Column(name = "performance_metrics", columnDefinition = "TEXT")
    private String performanceMetrics;

    /**
     * 关联日志ID
     */
    @Column(name = "related_log_id")
    private Long relatedLogId;

    /**
     * 父日志ID
     */
    @Column(name = "parent_log_id")
    private Long parentLogId;

    /**
     * 日志层级
     */
    @Column(name = "log_level_depth")
    private Integer logLevelDepth;

    /**
     * 是否为关键日志
     */
    @Column(name = "is_critical", nullable = false)
    private Boolean isCritical;

    /**
     * 是否需要关注
     */
    @Column(name = "requires_attention")
    private Boolean requiresAttention;

    /**
     * 是否已处理
     */
    @Column(name = "is_processed")
    private Boolean isProcessed;

    /**
     * 处理人ID
     */
    @Column(name = "processed_by")
    private Long processedBy;

    /**
     * 处理人姓名
     */
    @Column(name = "processed_by_name", length = 100)
    private String processedByName;

    /**
     * 处理时间
     */
    @Column(name = "processed_time")
    private LocalDateTime processedTime;

    /**
     * 处理备注
     */
    @Column(name = "processing_notes", length = 500)
    private String processingNotes;

    /**
     * 删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 服务器名称
     */
    @Column(name = "server_name", length = 100)
    private String serverName;

    /**
     * 应用实例ID
     */
    @Column(name = "application_instance_id", length = 100)
    private String applicationInstanceId;

}